﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Block b = new Block
            {
                BlockID = 1,
            };
            b.PropertyChanging += b_PropertyChanging;
            b.PropertyChanged += b_PropertyChanged;
            b.BlockID = 2;

            Console.ReadKey();
        }

        static void b_PropertyChanged(object sender, PropertyChangeEventArgs e)
        {
            Console.WriteLine("属性：{0}变化之前的值{1}", e.PropertyName, e.PropertyValue);
        }

        static void b_PropertyChanging(object sender, PropertyChangeEventArgs e)
        {
            Console.WriteLine("属性：{0}变化之后的值{1}", e.PropertyName, e.PropertyValue);
        }
    }


    public partial class Block : EntityBase
    {
        private int _BlockID;
        public int BlockID
        {
            get
            {
                return this._BlockID;
            }
            set
            {
                if ((this._BlockID != value))
                {

                    this.SendPropertyChanging("BlockID", this._BlockID);
                    this._BlockID = value;
                    this.SendPropertyChanged("BlockID", this._BlockID);

                }
            }
        }

    }

    public class EntityBase : INotifyPropertyChange
    {
        protected virtual void SendPropertyChanging(String propertyName, object propertyValue)
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, new PropertyChangeEventArgs(propertyName, propertyValue));
            }
        }

        protected virtual void SendPropertyChanged(String propertyName, object propertyValue)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangeEventArgs(propertyName, propertyValue));
            }
        }

        public event PropertyChangeEventHandler PropertyChanging;

        public event PropertyChangeEventHandler PropertyChanged;
    }


    // 摘要:
    //     表示将处理 System.ComponentModel.INotifyPropertyChanged.PropertyChanged 事件的方法，该事件在更改组件上的属性时引发。
    //
    // 参数:
    //   sender:
    //     事件源。
    //
    //   e:
    //     包含事件数据的 System.ComponentModel.PropertyChangedEventArgs。
    public delegate void PropertyChangeEventHandler(object sender, PropertyChangeEventArgs e);

    /// <summary>
    /// 记录类中属性修改过程的接口
    /// </summary>
    public interface INotifyPropertyChange
    {
        /// <summary>
        /// 修改之前的事件
        /// </summary>
        event PropertyChangeEventHandler PropertyChanging;
        /// <summary>
        /// 修改之后的事件
        /// </summary>
        event PropertyChangeEventHandler PropertyChanged;
    }

    /// <summary>
    /// 类中属性变化时所需要的事件数据
    /// </summary>
    public class PropertyChangeEventArgs : EventArgs
    {
        private string _propertyName;
        private object _propertyValue;
        public PropertyChangeEventArgs(string propertyName, object propertyValue)
        {
            this._propertyName = propertyName;
            this._propertyValue = propertyValue;
        }
        public virtual string PropertyName { get { return this._propertyName; } }
        public virtual object PropertyValue { get { return this._propertyValue; } }
    }

}
